package com.alphagen.sample.bestroute;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.alphagen.sample.bestroute.model.GasolineStation;
import com.alphagen.sample.bestroute.utils.PDFReader;

public class BestRouteSuggester {

	private InputStream inputFile = null;
	private ArrayList<GasolineStation> parsedInput = null;
	private String sourcePlace;
	private String destPlace;
	private JSONObject routeInfo;

	public String getSourcePlace() {
		return sourcePlace;
	}

	public void setSourcePlace(String sourcePlace) {
		this.sourcePlace = sourcePlace;
	}

	public String getDestPlace() {
		return destPlace;
	}

	public void setDestPlace(String destPlace) {
		this.destPlace = destPlace;
	}

	public InputStream getInputFile() {
		return inputFile;
	}

	public void setInputFile(InputStream inputFile) {
		this.inputFile = inputFile;
	}

	public ArrayList<GasolineStation> getParsedInput() throws IOException {
		if (parsedInput == null) {

			parsedInput = new ArrayList<GasolineStation>();

			PDFReader pdfParser = new PDFReader();
			JSONArray jsonArray = pdfParser.convertPDFToJSON(getInputFile());
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				GasolineStation gasoline = new GasolineStation();
				gasoline.setInfoDate(jsonObject.getString("DATE"));
				gasoline.setSt(jsonObject.getString("ST"));
				gasoline.setCity(jsonObject.getString("CITY"));
				gasoline.setPPG(jsonObject.getString("PPG"));
				gasoline.setName(jsonObject.getString("NAME"));
				gasoline.setAddress(jsonObject.getString("ADDRESS"));
				gasoline.setDirections(jsonObject.getString("DIRECTIONS"));
				if (gasoline.isDataValid()) {
					parsedInput.add(gasoline);
				}
			}
			System.out.println("inputJSON" + parsedInput);
		}
		return parsedInput;
	}

	public void setParsedInput(ArrayList<GasolineStation> parsedInput) {
		this.parsedInput = parsedInput;
	}

	public JSONObject getRouteInfo() throws IOException {
		if (routeInfo == null) {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(
					"https://maps.googleapis.com/maps/api/directions/json?quotaUser=AlphaGen&avoid=tolls&origin="
							+ getSourcePlace() + "&destination="
							+ getDestPlace() + "&sensor=false&waypoints=via:Plainfield");
			httpGet.getHeaders("");
			
			//&key=AIzaSyACedmxYdQtyGOlG6zB-xEFsSVL5fngms8
			CloseableHttpResponse response1 = httpclient.execute(httpGet);

			try {
				HttpEntity entity1 = response1.getEntity();

				String strResponse = EntityUtils.toString(entity1);
				routeInfo = new JSONObject(strResponse);

				EntityUtils.consume(entity1);
			} finally {
				response1.close();
			}

		}
		return routeInfo;
	}
}
